package com.dubbo.consumer;

import com.google.common.collect.Maps;
import com.google.common.io.Files;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author carter
 * create time    2018-07-24
 */
public class ConumerUtil {

    public static void main(String[] args) {


        Map<String,List<String>> map = Maps.newHashMap();
        map.put("admin",Arrays.asList("admin","olderp"));

        map.put("product",Arrays.asList("product"));
        map.put("orders",Arrays.asList("order","crm"));

        map.put("purchase",Arrays.asList("purchase"));
        map.put("store",Arrays.asList("store"));
        map.put("tms",Arrays.asList("tms"));
        map.put("deamon",Arrays.asList("deamon"));


        for (String key: map.keySet())
        {
            List<String> subModuleList = map.get(key);

            for (String subModule: subModuleList)
            {

                String  serviceJavaFilePath=String.format("D:\\workspace\\erp-v2\\backend-exts\\%s-ext\\src\\main\\java\\com\\stosz\\%s\\ext\\service",key,subModule);
                generateDubboConsumerFile(serviceJavaFilePath);
            }
        }



    }

    private static void generateDubboConsumerFile(String serviceJavaFilePath) {
        File serviceDir = new File(serviceJavaFilePath);


        if (!serviceDir.exists())
        {
            System.out.println("目录不存在 " + serviceDir.getAbsolutePath());

            return;
        }


        String moduleName = serviceDir.getParentFile().getParentFile().getName();

        System.out.println("模块名称：" + moduleName);


        StringBuffer stringBuffer = new StringBuffer();

        stringBuffer.append("package com.stosz;\n\n");

        stringBuffer.append("import com.stosz."+moduleName+".ext.service.*;\n");
        stringBuffer.append("import org.springframework.context.annotation.Bean;\n" +
                "import com.alibaba.dubbo.config.annotation.Reference;\n");

        stringBuffer.append("\n/**\n" +
                " * "+moduleName+"模块dubbo消费者配置\n" +
                " * @author carter\n" +
                " * create time    "+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))+"\n" +
                " */\n");

        String moduleFirstToCase = moduleName.replaceFirst(moduleName.substring(0,1),moduleName.substring(0,1).toUpperCase());
        stringBuffer.append("public abstract class "+ moduleFirstToCase+"DubboConsumer {\n\n");

        Arrays.stream(serviceDir.list()).forEachOrdered(fileName->{

            if (!fileName.startsWith("package-info"))
            {



            System.out.println(fileName);

            String serviceName = fileName.substring(0, fileName.lastIndexOf("."));

            stringBuffer.append("\t@Reference\n");
            stringBuffer.append("\tprivate "+serviceName + "  instanceOf" + serviceName+ ";\n");
            stringBuffer.append("\n\t@Bean\n");
            stringBuffer.append("\tpublic "+serviceName+" getInstanceOf"+serviceName+"(){\n");
            stringBuffer.append("\t\treturn  instanceOf"+serviceName+";\n");
            stringBuffer.append("\t}\n\n");


            }


        });

        stringBuffer.append("\n}");


        String fileName = moduleFirstToCase+"DubboConsumer.java";

        String saveFileName = serviceDir.getParentFile().getParentFile().getParentFile().getAbsolutePath()+"\\"+fileName;


        System.out.println("保存的文件名是：" + saveFileName);

        File saveFile = new File(saveFileName);
        if (saveFile.exists())
        {
            saveFile.delete();
        }

        try {
            saveFile.createNewFile();
            Files.write(stringBuffer.toString().getBytes(Charset.forName("utf8")),saveFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
